package com.appengine.utils;
import com.datatypes.AEuser;
import com.datatypes.FriendsZone;
import com.datatypes.User;
import com.sporteamws.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.app.ProgressDialog;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class UIUtils 
{
  /* prints a toast on a UI thread in the background
     parameters
     c - the context which the toast will be printed too
     msg - the toast msg
     toast_length - can be Toast.LENGTH_SHORT/Toast.LENGTH_LONG
  */  
  static ProgressDialog loadingBar;
  
  public static void PrintToast(Context context,String msg,int toast_length)
  {
	  //setup final variables for thread
	  final Context f_context = context;
	  final Activity f_act = (Activity)context;
	  final String f_msg = msg;
	  final int f_toast_length = toast_length;
 
	  //create new thread with as UI thread execution
	  Thread t = new Thread()
	  {
		    public void run()
		    {
		    	f_act.runOnUiThread(new Runnable()
		        {
		            @Override
		            public void run() 
		            {
		            	Toast.makeText(f_context, f_msg, f_toast_length).show();
		            }
		        });
		    }
      };
      
      //execute thread
	  t.start();
  }
  
  public static void SetTextOnUIthread(Context context,TextView tv,String text)
  {
	  //setup final variables for thread
	  final Context f_context = context;
	  final Activity f_act = (Activity)context;
	  final String f_text = text;
	  final TextView f_tv = tv;
 
	  //create new thread with as UI thread execution
	  Thread t = new Thread()
	  {
		    public void run()
		    {
		    	f_act.runOnUiThread(new Runnable()
		        {
		            @Override
		            public void run() 
		            {
		            	f_tv.setText(f_text);
		            }
		        });
		    }
      };
      
      //execute thread
	  t.start();
  }
  
  public static void showProgressBar(ProgressBar PB,Context context)
  {
	  //setup final variables for thread
	  final ProgressBar f_PB = PB;
	  final Activity f_act = (Activity)context;
 
	  //create new thread with as UI thread execution
	  Thread t = new Thread()
	  {
		    public void run()
		    {
		    	f_act.runOnUiThread(new Runnable()
		        {
		            @Override
		            public void run() 
		            {
		            	f_PB.setVisibility(View.VISIBLE);
		            }
		        });
		    }
      };
      
      //execute thread
	  t.start();
  }
  
  public static void hideProgressBar(ProgressBar PB,Context context)
  {
	//setup final variables for thread
	  final ProgressBar f_PB = PB;
	  final Activity f_act = (Activity)context;
 
	  //create new thread with as UI thread execution
	  Thread t = new Thread()
	  {
		    public void run()
		    {
		    	f_act.runOnUiThread(new Runnable()
		        {
		            @Override
		            public void run() 
		            {
		            	f_PB.setVisibility(View.INVISIBLE);
		            }
		        });
		    }
      };
      
      //execute thread
	  t.start();
  }
  
  public static void ShowLoadingBar(final Context context)
  {
	  final Activity f_act = (Activity)context;
	  //create new thread with as UI thread execution
	  Thread t = new Thread()
	  {
		    public void run()
		    {
		    	f_act.runOnUiThread(new Runnable()
		        {
		            @Override
		            public void run() 
		            {
		            	loadingBar = new ProgressDialog(context);
		            	loadingBar.requestWindowFeature(Window.FEATURE_NO_TITLE);
		            	loadingBar.setMessage("Loading...");
		            	loadingBar.show();
		            }
		        });
		    }
      };
      
      //execute thread
	  t.start();
  }
  
  public static void HideLoadingBar(Context context)
  {
	  final Activity f_act = (Activity)context;
	  //create new thread with as UI thread execution
	  Thread t = new Thread()
	  {
		    public void run()
		    {
		    	f_act.runOnUiThread(new Runnable()
		        {
		            @Override
		            public void run() 
		            {
		            	loadingBar.dismiss();
		            }
		        });
		    }
      };
      
      //execute thread
	  t.start();
  }
  
  public static void SetBitmapPic(ImageView img,Bitmap bitmap_to_set, Context context)
  {
	  final ImageView f_img = img;
	  final Bitmap f_bitmap_to_set = bitmap_to_set;
	  
	  final Activity f_act = (Activity)context;
	  
	  Thread t = new Thread()
	  {
		    public void run()
		    {
		    	f_act.runOnUiThread(new Runnable()
		        {
		            @Override
		            public void run() 
		            {
		            	f_img.setImageBitmap(f_bitmap_to_set);
		            }
		        });
		    }
	  };
	  
      //execute thread
	  t.start();
  }
  
  
  public static void CacheUserData(AEuser ae_user,User user,Context c)
  {
	  final AEuser f_ae_user = ae_user;
	  final User f_user = user;
	  final Context f_c = c;
	  final Activity f_act = (Activity)c;
	  
	  Thread t = new Thread()
	  {
		    public void run()
		    {
		    	f_act.runOnUiThread(new Runnable()
		        {
		            @Override
		            public void run() 
		            {
		            	// save tracks
		        		LogUtils.SendMsgLog("saving tracks..");
		        		SavingAndLoadingCenter.saveUserTracks(f_user.GetFacebookID(),
		        				f_ae_user.GetTracks(), f_c);

		        		// save activities
		        		LogUtils.SendMsgLog("saving activities");
		        		SavingAndLoadingCenter.saveActivityTracks(f_user.GetFacebookID(),
		        				f_ae_user.GetActivities(), f_c);

		        		// save user
		        		LogUtils.SendMsgLog("saving user..");
		        		SavingAndLoadingCenter.saveUser(f_user, f_c);

		        		// save friends zone
		        		if (f_ae_user.GetSporTeamFriends() == null)
		        			f_ae_user.InitSporTeamFriends();

		        		FriendsZone fz = new FriendsZone(f_ae_user.GetSporTeamFriends(),
		        				f_ae_user.GetActivities());
		        		LogUtils.SendMsgLog("saving friends..");
		        		SavingAndLoadingCenter.saveFriendsZone(f_ae_user.GetFacebookID(), fz,f_c);
		            }
		        });
		    }
      };
      
      //execute thread
	  t.start();
	  
  }
  
  public static void showAlertDialog(Context c,String title,String msg)
  {
	 final Context f_c = c;
	 final Activity f_act = (Activity)c;
	 final String f_title = title;
	 final String f_msg = msg;
	 final AlertDialog alertDialog = new AlertDialog.Builder(f_c).create();
	 
	 alertDialog.setTitle(f_title);
	  alertDialog.setMessage(f_msg);
	  alertDialog.setButton("exit", new DialogInterface.OnClickListener() {
	     public void onClick(DialogInterface dialog, int which) {
	    	 alertDialog.dismiss();
	    	 f_act.finish();
	     }
	  });
	  
	  alertDialog.setIcon(R.drawable.solo);
	  
	  Thread t = new Thread()
	  {
		    public void run()
		    {
		    	f_act.runOnUiThread(new Runnable()
		        {
		            @Override
		            public void run() 
		            {
		            	  alertDialog.show();
		            }
		        });
		    }
      };
      
	  
	
	  t.start();
  }
 
  
  
}
